package cn.com.self.apis.escrow.controller;


import cn.com.self.apis.escrow.entity.EscrowInfo;
import cn.com.self.apis.escrow.service.IEscrowInfoService;
import cn.com.self.apis.portal.util.ICustomPage;
import cn.com.selfsoft.smartcity.commons.http.DataItem;
import cn.com.selfsoft.smartcity.commons.http.DataPage;
import cn.com.selfsoft.smartcity.commons.http.ResultModel;
import cn.com.selfsoft.smartcity.commons.util.ParameterUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author liangwt
 * @since 2020-05-28
 */
@Controller
@RequestMapping("/escrowInfo")
@Api(tags = "托管信息类")
public class EscrowInfoController {
    @Autowired
    private IEscrowInfoService escrowInfoService;

    /**
     * 新增托管
     *
     * @return
     * @Description //TODO
     **/
    @RequestMapping(value = "/create", method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation(value = "新增托管信息")
    @Transactional
    public ResponseEntity create(@RequestBody EscrowInfo entity, HttpServletRequest request) {
        ResponseEntity create = escrowInfoService.createEscrowInfo(entity);
        //entity.setCreateDate(new Date());
        //entity.setUpdateDate(new Date());
        //entity.setProgress("0%");
        //EscrowInfo create = escrowInfoService.create(entity);
        return create;
    }


    /**
     * 分页获取托管列表
     *
     * @return
     * @Description //TODO
     **/
    @ApiOperation(value = "分页获取托管列表")
    @RequestMapping(value = "/page", method = RequestMethod.GET)
    @ResponseBody
    public ResponseEntity getEscrowInfoPage(HttpServletRequest request, Pageable page) {
        Map<String, Object> queryParams = ParameterUtil.HumpToUnderline(request.getParameterMap(), "mark", "size", "page", "sort", "access_token");
        ICustomPage<EscrowInfo> pageData = this.escrowInfoService.page(queryParams, page);
        return ResponseEntity.status(HttpStatus.OK).body(new ResultModel<>(pageData));

    }

    /**
     * 获取托管信息
     *
     * @return
     * @Description //TODO
     **/
    @ApiOperation(value = "获取托管信息")
    @RequestMapping(value = {"/load/{id}"}, method = RequestMethod.GET)
    @ResponseBody
    public ResponseEntity loadEscrowInfo(@PathVariable("id") String id, HttpServletRequest request) {
        EscrowInfo escrowInfo = this.escrowInfoService.loadEscrowInfoById(id);
        return ResponseEntity.status(HttpStatus.OK).body(new ResultModel<>(new DataItem(escrowInfo)));

    }


    /**
     * 修改托管信息
     *
     * @return
     * @Description //TODO
     **/
    @ApiOperation(value = "修改托管信息")
    @RequestMapping(value = {"/update/{id}"}, method = RequestMethod.POST)
    @ResponseBody
    @Transactional
    public ResponseEntity update(@PathVariable("id") String id, @RequestBody EscrowInfo entity, HttpServletRequest request) {
        ResponseEntity update = this.escrowInfoService.updateEscrowInfo(id, entity);
        return update;

    }


    /**
     * 删除托管信息
     *
     * @return
     * @Description //TODO
     **/
    @ApiOperation(value = "删除托管信息")
    @ResponseBody
    @RequestMapping(value = "/delete/{id}", method = RequestMethod.DELETE)
    @Transactional
    public ResponseEntity deletes(@PathVariable("id") String id) {
        this.escrowInfoService.delete(id);
        this.escrowInfoService.deleteUFile(id);
        return ResponseEntity.status(HttpStatus.OK).body(new ResultModel<>(1));
    }
}

